package com.xb.p3_fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;


//3.Activity实现接口
public class MainActivity extends AppCompatActivity implements ListviewFragment.ListViewEvent {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*动态添加fragment：使用fragmentManager片段管理器*/
        /*FragmentManager fm=getSupportFragmentManager();
        fm.beginTransaction().add(R.id.list_fragment,new ListviewFragment()).commitNow();//开启事务*/

        /*上述代码编写存在问题，当手机横屏时出错，原因是，横屏时再次执行new ListviewFragment()创建对象*/


        /*判断是横屏还是竖屏,通过获取屏幕的像素实现*/
        DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth=dm.widthPixels;
        int screenHeight=dm.heightPixels;

        FragmentManager fm=getSupportFragmentManager();
        //获取添加进去的片段
        Fragment frag=fm.findFragmentById(R.id.list_fragment);
        if(screenWidth>screenHeight){//宽>高，横屏
            if(frag !=null){
                fm.beginTransaction().remove(frag).commitNow();
            }
        }else{
            if( frag == null){
                fm.beginTransaction().add(R.id.list_fragment,new ListviewFragment()).commitNow();//开启事务
            }
        }
    }

    //fragment通信方式一：由Activity进行Fragment的事件处理
    private int img[]={R.id.image0,R.id.image1,R.id.image2,R.id.image3,R.id.image4,R.id.image5,R.id.image6};
    @Override
    protected void onStart() {
        super.onStart();
        /*//要填充图片的View的ID
        final ImageView img_frag=findViewById(R.id.image_view);
        for (int i = 0; i < img.length; i++) {
            ImageView iv=findViewById(img[i]);
            if(iv !=null){
                //为List中的每一个图片添加监听事件
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //点击时，将当前图片的View填充到指定位置
                        ImageView view=(ImageView) v;
                        img_frag.setImageDrawable(view.getDrawable());
                    }
                });
            }
        }*/

        //通信方式三中的初始化：
        img_frag=findViewById(R.id.image_view);
    }

    ////3.Activity实现接口,重写方法
    //全局定义，onstart中初始化
    ImageView img_frag;
    @Override
    public void onImageClickListner(ImageView imageView) {
        img_frag.setImageDrawable(imageView.getDrawable());
    }

    //5.Activity的onAttach方法中,调用Fragment 设置接口的方法,将自身引用传入
    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if(fragment instanceof ListviewFragment){
            ListviewFragment lvf= (ListviewFragment)fragment;
            lvf.setListViewEvent(this);
        }
    }
}
